package com.zhanglinwei.openApiClient.segment;

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.zhanglinwei.common.constants.StringPoolConstant.COMMA_SPACE;

@Getter
@Setter
public class GroupBySegment implements SQLSegment{

    private final List<String> groupBySegmentList = new ArrayList<>();

    @Override
    public boolean isEmpty() {
        return CollectionUtils.isEmpty(groupBySegmentList);
    }

    @Override
    public void add(SQLSegment... sqlSegments) {
        if (ArrayUtils.isNotEmpty(sqlSegments)) {
            Arrays.asList(sqlSegments).forEach(item -> groupBySegmentList.add(item.sqlSegment()));
        }
    }

    @Override
    public String sqlSegment() {
        return SQLKeyword.GROUP_BY.sqlSegment() + " " + String.join(COMMA_SPACE, groupBySegmentList);
    }
}
